#ifndef CONFIG_H
#define CONFIG_H

#define BUFF_SIZE 5100
//#define BLOCK_SIZE 512
#define USE_DEBUG

#define USE_PACKAGE
/*
struct PACKAGE_INDEX_LEN
{
	const int STATE = 1;
	const int ACTION = 2;
	const int HEADER = 8;
	//const int MESSAGE = 
};

struct PACKAGE_INDEX
{
	const int STATE = 0;
	const int ACTION = STATE + PACKAGE_INDEX_LEN.STATE;
	const int HEADER = ACTION + PACKAGE_INDEX_LEN.ACTION;
	const int MESSAGE = HEADER + PACKAGE_INDEX_LEN.HEADER;
};
*/
#define IDX_STATE = 0;
#define IDX_ACTION = IDX_STATE + 1;
#define IDX_HEADER = IDX_ACTION + 2;
#define IDX_MESSAGE = IDX_HEADER + 8;

#define STATE_SIGN_IN 0
#define STATE_SIGN_OUT 1
#define STATE_SEND 2

#define ACTION_SEND = 0;

static int ReadINT32(byte* buff, int offset)
{
	int result = 0;
	memcpy(&result, buff + offset, 4);
	return result;
}


static void WriteINT32(int n, byte* buff, int offset)
{
	memset(buff + offset, 0, 4);
	memcpy(buff + offset, &n, 4);
}

static short ReadINT16(byte* buff, int offset)
{
	short result = 0;
	memcpy(&result, buff + offset, 2);
	return result;
}


static void WriteINT16(short n, byte* buff, int offset)
{
	memset(buff + offset, 0, 2);
	memcpy(buff + offset, &n, 2);
}

static byte ReadINT8(byte* buff, int offset)
{
	return buff[offset];
}


static void WriteINT8(byte n, byte* buff, int offset)
{
	buff[offset] = n;
}

struct Package
//class Package
{
public:
	byte type;
	byte action;
	int dataSize;

	int dataOffset;
	byte data[BUFF_SIZE - 10];
	Package()
	{
		//dataOffset = sizeof(byte) + sizeof(byte) + sizeof(int);
		OutputDebugString(_T("[Server] Package constructor!"));
		dataOffset = 6;
	}
	void LoadData(byte* buff, int offset = 0)
	{
		OutputDebugString(_T("----- Package load data start -----"));
		int _offset = offset;

		type = ReadINT8(buff, _offset);
		_offset += 1;

		action = ReadINT8(buff, _offset);
		_offset += 1;

		dataSize = ReadINT32(buff, _offset);
		_offset += 4;
OutputDebugString(_T("+++ memcpy start +++"));
char str[256];
sprintf(str, "*** type %d, action %d, dataSize %d", type, action, dataSize);
OutputDebugStringA(str);
		memcpy(data, buff + _offset, dataSize);
		OutputDebugString(_T("----- Package load data END -----"));
	}

	int WriteToBuffer(byte* buff)
	//int WriteToBuffer(byte buff[])
	{
		int offset = 0;

		WriteINT8(type, buff, offset);
		offset++;

		WriteINT8(action, buff, offset);
		offset++;

		if(dataSize < 0 || dataSize > sizeof(data))
			dataSize = sizeof(data);
		WriteINT32(dataSize, buff, offset);		
		offset+=4;
		/*
#ifdef USE_DEBUG
	OutputDebugString(_T("^^^^^^^^^^^^^^^^^^^^^^^^^^^^"));
	char str[256];
	sprintf(str, "Sizeof buffer:%d", sizeof(buff));
	OutputDebugStringA(str);

	sprintf(str, "Offset:%d", offset);
	OutputDebugStringA(str);

	sprintf(str, "Data size:%d", dataSize);
	OutputDebugStringA(str);
#endif
	*/
		memcpy(buff + offset, data, dataSize);

		return offset + dataSize;
	}
};

enum TYPE
{
	CHATTING = 1,
	FILE_TRANSFER = 2, 
};

enum ACTION
{
	CHAT_SIGNIN = 1,
	CHAT_SEND_MESSAGE = 2,
	CHAT_SIGNOUT = 3,

	FILE_HEADER = 1,
	FILE_START = 2,
	FILE_SEND = 3,
	FILE_STOP = 4,
};
#endif